package com.dhcc.sds.standard.controller;


import java.util.List;
import java.util.Map;


import com.dhcc.core.framework.result.node.ZTreeNode;
import com.dhcc.sds.standard.entity.SdsStandDicEntity;
import com.dhcc.sds.standard.service.ISdsStandDicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;



import com.dhcc.sds.standard.entity.SdsStandDicsubEntity;
import com.dhcc.sds.standard.service.ISdsStandDicsubService;


/**
 * 项目标准化字典子项
 * 
 * @ClassName: SdsStandDicsubController
 * @Description: TODO
 * @author sunyingjie
 * @date 2021-03-22 10:09:53
 */
@Controller
@RequestMapping("${sys.backendPath}/sds/sdsstanddicsub")
public class SdsStandDicsubController extends BaseController {

	private String PREFIX = "/backend/sds/sdsstanddicsub/";

	@Autowired
	private ISdsStandDicsubService sdsStandDicsubService;

    @Autowired
    private ISdsStandDicService sdsStandDicService;
	

    /**
     * 跳转到项目标准化字典子项列表首页
     */
    @RequestMapping("")
    public String index(Model model) {
        model.addAttribute("dicMap",sdsStandDicService.getDicMap());
        return PREFIX + "sdsstanddicsub.html";
    }
	
    /**
     * 跳转到新增项目标准化字典子项
     */
    @RequestMapping("/to_add")
    public String toAdd(Model model) {
        model.addAttribute("dicMap",sdsStandDicService.getDicMap());
        return PREFIX + "sdsstanddicsub_add.html";
    }
	
	/**
     * 跳转到修改项目标准化字典子项
     */
    @RequestMapping("/to_update/{sdsStandDicsubId}")
    public String toUpdate(@PathVariable Long sdsStandDicsubId, Model model) {
        SdsStandDicsubEntity sdsStandDicsub = this.sdsStandDicsubService.selectById(sdsStandDicsubId);
        model.addAttribute("sdsStandDicsub",sdsStandDicsub);
        model.addAttribute("dicMap",sdsStandDicService.getDicMap());
        return PREFIX + "sdsstanddicsub_edit.html";
    }

    /**
     * 获取项目标准化字典子项列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam Map<String, Object> params) {
		Page<SdsStandDicsubEntity> page = new PageFactory<SdsStandDicsubEntity>().defaultPage();
        List<SdsStandDicsubEntity> result = this.sdsStandDicsubService.page(page,params);
        page.setRecords((List<SdsStandDicsubEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 新增项目标准化字典子项
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(SdsStandDicsubEntity sdsStandDicsub) {
        if (CommonUtil.isOneEmpty(sdsStandDicsub)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        sdsStandDicsub.insert();
        return R.ok();
    }

    /**
     * 删除项目标准化字典子项
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Long sdsStandDicsubId) {
	
        this.sdsStandDicsubService.deleteById(sdsStandDicsubId);

        return R.ok();
    }

    /**
     * 修改项目标准化字典子项
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(SdsStandDicsubEntity sdsStandDicsub) {
        if (CommonUtil.isOneEmpty(sdsStandDicsub)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        SdsStandDicsubEntity old = this.sdsStandDicsubService.selectById(sdsStandDicsub.getId());

        old.setDicId(sdsStandDicsub.getDicId());
        old.setDicCode(sdsStandDicsub.getDicCode());
        old.setDicDesc(sdsStandDicsub.getDicDesc());
        old.setResume(sdsStandDicsub.getResume());
        old.updateAllColumnById();
        return R.ok();
    }


    /**
     * 根据数据源获取项目标准化字典子项列表
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/listBySource")
    @ResponseBody
    public Object listBySource(@RequestParam Map<String, Object> params, String source) {
        Page<SdsStandDicsubEntity> page = new PageFactory<SdsStandDicsubEntity>().defaultPage();
        List<SdsStandDicEntity> sdsStandDicEntities = sdsStandDicService.listBySource(source);
        List<SdsStandDicsubEntity> result = this.sdsStandDicsubService.pageByDic(page,params,sdsStandDicEntities);
        page.setRecords((List<SdsStandDicsubEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 获取字典子项的tree列表
     */
    @RequestMapping(value = "/tree/{sourceDesc}")
    @ResponseBody
    public List<ZTreeNode> entityTree(@PathVariable String sourceDesc) {
        return sdsStandDicsubService.getEntityTree(sourceDesc);
    }
	
}
